# executed by mmendozaavina@fas.harvard.edu on 2023-12-07
# see environment information pasted at the bottom



library("haven")
library("scales")
library("modelsummary")
library("lmtest")
library("margins")
library("MASS")
library("sandwich")
library("patchwork")
library("matrixStats")
library("kableExtra")
library("tidyverse")



# load data
d <- read_rds("data.rds")





# PRE-REGISTERED MODELS -----------------------------------------------------

models_ols <- list(
  lm(dv_agree ~ t1_info + t2_frame, 
     weights = weight, data = subset(d, !is.na(pid))),
  lm(dv_agree ~ t1_info + t2_frame + pid
     + age + gender + race + education + income + married
     + employment + immigrant + francophone + region,
     weights = weight, data = d),
  lm(dv_angry ~ t1_info + t2_frame, 
     weights = weight, data = subset(d, !is.na(pid))),
  lm(dv_angry ~ t1_info + t2_frame + pid
     + age + gender + race + education + income + married
     + employment + immigrant + francophone + region, 
     weights = weight, data = d),
  lm(dv_agree ~ t1_info + t2_frame, 
     weights = weight, data = subset(d, pid == 0)),
  lm(dv_agree ~ t1_info + t2_frame
     + age + gender + race + education + income + married
     + employment + immigrant + francophone + region, 
     weights = weight, data = subset(d, pid == 0)),    
  lm(dv_agree ~ t1_info + t2_frame, 
     weights = weight, data = subset(d, pid == 1)),
  lm(dv_agree ~ t1_info + t2_frame
     + age + gender + race + education + income + married
     + employment + immigrant + francophone + region, 
     weights = weight, data = subset(d, pid == 1)),
  lm(dv_angry ~ t1_info + t2_frame, 
     weights = weight, data = subset(d, pid == 0)),
  lm(dv_angry ~ t1_info + t2_frame
     + age + gender + race + education + income + married
     + employment + immigrant + francophone + region, 
     weights = weight, data = subset(d, pid == 0)),
  lm(dv_angry ~ t1_info + t2_frame, 
     weights = weight, data = subset(d, pid == 1)),
  lm(dv_angry ~ t1_info + t2_frame
     + age + gender + race + education + income + married
     + employment + immigrant + francophone + region, 
     weights = weight, data = subset(d, pid == 1))
)

models_vote <- list(
  lm(vote_post ~ dv_agree 
     + t1_info + t2_frame + pid
     + ideology_left + ideology_right
     + satisfaction_trudeau
     + age + gender + race + education + income + married
     + employment + immigrant + francophone + region, 
     weights = weight, data = subset(d, !is.na(pid))),
  lm(vote_post ~ dv_angry 
     + t1_info + t2_frame + pid 
     + ideology_left + ideology_right
     + satisfaction_trudeau
     + age + gender + race + education + income + married
     + employment + immigrant + francophone + region, 
     weights = weight, data = subset(d, !is.na(pid))),
  lm(vote_post ~ dv_agree 
     + t1_info + t2_frame
     + ideology_left + ideology_right
     + satisfaction_trudeau
     + age + gender + race + education + income + married
     + employment + immigrant + francophone + region, 
     weights = weight, data = subset(d, pid == 0)),
  lm(vote_post ~ dv_angry 
     + t1_info + t2_frame
     + ideology_left + ideology_right
     + satisfaction_trudeau
     + age + gender + race + education + income + married
     + employment + immigrant + francophone + region, 
     weights = weight, data = subset(d, pid == 0)),
  lm(vote_post ~ dv_agree 
     + t1_info + t2_frame
     + ideology_left + ideology_right
     + satisfaction_trudeau
     + age + gender + race + education + income + married
     + employment + immigrant + francophone + region, 
     weights = weight, data = subset(d, pid == 1)),
  lm(vote_post ~ dv_angry 
     + t1_info + t2_frame
     + ideology_left + ideology_right
     + satisfaction_trudeau
     + age + gender + race + education + income + married
     + employment + immigrant + francophone + region, 
     weights = weight, data = subset(d, pid == 1))
)

models_cate <- list(
  lm(dv_agree ~ t1_info * awareness_index + t2_frame * awareness_index
     + age + gender + race + education + income + married
     + employment + immigrant + francophone + region + pid, 
     weights = weight, data = d),
  lm(dv_agree ~ t1_info * cynicism_index + t2_frame * cynicism_index
     + age + gender + race + education + income + married
     + employment + immigrant + francophone + region + pid, 
     weights = weight, data = d),
  lm(dv_agree ~ t1_info * economy_index + t2_frame * economy_index
     + age + gender + race + education + income + married
     + employment + immigrant + francophone + region + pid, 
     weights = weight, data = d),
  lm(dv_angry ~ t1_info * awareness_index + t2_frame * awareness_index
     + age + gender + race + education + income + married
     + employment + immigrant + francophone + region + pid, 
     weights = weight, data = d),
  lm(dv_angry ~ t1_info * cynicism_index + t2_frame * cynicism_index
     + age + gender + race + education + income + married
     + employment + immigrant + francophone + region + pid, 
     weights = weight, data = d),
  lm(dv_angry ~ t1_info * economy_index + t2_frame * economy_index
     + age + gender + race + education + income + married
     + employment + immigrant + francophone + region + pid, 
     weights = weight, data = d)
)

models_glm <- list(
  coeftest(polr(dv_agree_factor ~ t1_info + t2_frame, 
                weights = weight, data = subset(d, !is.na(pid)), 
                Hess=TRUE), vcov = sandwich),
  coeftest(polr(dv_agree_factor ~ t1_info + t2_frame + pid
                + age + gender + race + education + income + married
                + employment + immigrant + francophone + region, 
                weights = weight, data = d, 
                Hess=TRUE), vcov = sandwich),
  coeftest(polr(dv_angry_factor ~ t1_info + t2_frame, 
                weights = weight, data = subset(d, !is.na(pid)), 
                Hess=TRUE), vcov = sandwich),
  coeftest(polr(dv_angry_factor ~ t1_info + t2_frame + pid
                + age + gender + race + education + income + married
                + employment + immigrant + francophone + region, 
                weights = weight, data = d, 
                Hess=TRUE), vcov = sandwich),
  coeftest(polr(dv_agree_factor ~ t1_info + t2_frame, 
                weights = weight, data = subset(d, pid == 0), 
                Hess=TRUE),  vcov = sandwich),
  coeftest(polr(dv_agree_factor ~ t1_info + t2_frame
                + age + gender + race + education + income + married
                + employment + immigrant + francophone + region, 
                weights = weight, data = subset(d, pid == 0), 
                Hess=TRUE), vcov = sandwich),
  coeftest(polr(dv_agree_factor ~ t1_info + t2_frame, 
                weights = weight, data = subset(d, pid == 1), 
                Hess=TRUE),  vcov = sandwich),
  coeftest(polr(dv_agree_factor ~ t1_info + t2_frame
                + age + gender + race + education + income + married
                + employment + immigrant + francophone + region, 
                weights = weight, data = subset(d, pid == 1), 
                Hess=TRUE), vcov = sandwich),
  coeftest(polr(dv_angry_factor ~ t1_info + t2_frame, 
                weights = weight, data = subset(d, pid == 0), 
                Hess=TRUE),  vcov = sandwich),
  coeftest(polr(dv_angry_factor ~ t1_info + t2_frame
                + age + gender + race + education + income + married
                + employment + immigrant + francophone + region, 
                weights = weight, data = subset(d, pid == 0), 
                Hess=TRUE), vcov = sandwich),
  coeftest(polr(dv_angry_factor ~ t1_info + t2_frame, 
                weights = weight, data = subset(d, pid == 1), 
                Hess=TRUE),  vcov = sandwich),
  coeftest(polr(dv_angry_factor ~ t1_info + t2_frame
                + age + gender + race + education + income + married
                + employment + immigrant + francophone + region, 
                weights = weight, data = subset(d, pid == 1), 
                Hess=TRUE), vcov = sandwich)
)


# MAIN TEXT RESULTS ---------------------------------------------------------

cm <- c("(Intercept)" = "Constant",
        "t1_info" = "T1: Information",
        "t2_frame" = "T2: Framing",
        "dv_agree" = "Agree",
        "dv_angry" = "Angry",
        "dv_agree:pid" = "Agree × Opposition",
        "dv_angry:pid" = "Angry × Opposition",
        "pid" = "PID (Opposition = 1, Liberal = 0)",
        "t1_info:pid" = "Information × PID",
        "pid:t2_frame" = "Framing × PID",
        "awareness_index" = "Political Awareness (Low = 1)",
        "t1_info:awareness_index" = "Information × Awareness",
        "awareness_index:t2_frame" = "Framing × Awareness",
        "cynicism_index" = "Political Cynicism (High = 1)",
        "t1_info:cynicism_index" = "Information × Cynicism",
        "cynicism_index:t2_frame" = "Framing × Cynicism",
        "economy_index" = "Economic Evaluations (Worse = 1)",
        "t1_info:economy_index" = "Information × Economic Evaluations",
        "economy_index:t2_frame" = "Framing × Economic Evaluations",
        "satisfaction_trudeau" = "Government Satisfaction",
        "ideology_left" = "Ideology: Left",
        "ideology_right" = "Ideology: Right")

mytheme <- theme_bw(base_size = 10) +
  theme(plot.title = element_text(hjust = 0.5, size = 9),
        axis.title.x = element_text(size = 9),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.title=element_blank(),
        legend.position = "bottom",
        legend.margin=margin(0,0,0,0),
        legend.box.margin=margin(-10,-10,-10,-10))


## figure 1: experimental treatment effects ----------------------------

modelplot(models_ols, 
          coef_map = c("t1_info" = "T1: Info",
                       "t2_frame" = "T2: Frame"),
          coef_omit = "pid|Interc",
          vcov = "robust",
          draw = FALSE) %>%
  mutate(model = str_replace_all(model, "[()]", ""),
         model = as.numeric(model),
         dv = ifelse(model %in% c(1:2, 5:8), "DV: Agree", "DV: Angry"),
         controls = ifelse(model %% 2 == 0, "With Covariates", "No Covariates"),
         pid = case_when(model == 1 ~ "All Respondents",
                         model == 2 ~ "All Respondents",
                         model == 3 ~ "All Respondents",
                         model == 4 ~ "All Respondents",
                         model == 5 ~ "Liberals Only",
                         model == 6 ~ "Liberals Only",
                         model == 7 ~ "Opposition Only",
                         model == 8 ~ "Opposition Only",
                         model == 9 ~ "Liberals Only",
                         model == 10 ~ "Liberals Only",
                         model == 11 ~ "Opposition Only",
                         model == 12 ~ "Opposition Only")) %>%
  ggplot(aes(x = estimate, y = reorder(term, desc(term)), 
             xmin = conf.low, xmax = conf.high,
             shape = controls)) +
  facet_grid(pid ~ dv) +
  geom_vline(xintercept = 0, 
             linetype="dotted", 
             color = 'black') +
  geom_errorbar(width = 0,
                position=position_dodge(width = -.5)) +
  geom_point(size = 2,
             position=position_dodge(width = -.5)) +
  ylab("") +
  xlab("Coefficient estimates with 95% confidence intervals") +
  coord_cartesian(xlim = c(-.15, .15)) +
  scale_fill_grey() + 
  mytheme

ggsave("ate_cate.pdf", width = 6, height = 5)


## figure 2: vote choice models -------------------------------------
modelplot(models_vote, 
          coef_map = cm,
          coef_omit = "[^dv_agree|dv_angry]",
          vcov = "robust",
          draw = FALSE) %>%
  mutate(model = str_replace_all(model, "[()]", ""),
         model = as.numeric(model),
         pid = case_when(model == 1 ~ "All Respondents",
                         model == 2 ~ "All Respondents",
                         model == 3 ~ "Liberals Only",
                         model == 4 ~ "Liberals Only",
                         model == 5 ~ "Opposition Only",
                         model == 6 ~ "Opposition Only")) %>%
  ggplot(aes(x = estimate, y = reorder(term, desc(term)), 
             xmin = conf.low, xmax = conf.high)) +
  facet_grid(pid ~ .) +
  geom_vline(xintercept = 0, 
             linetype="dotted", 
             color = 'black') +
  geom_errorbar(width = 0) +
  geom_point(size = 2) +
  ylab("") +
  xlab("Coefficient estimates with 95% confidence intervals") +
  coord_cartesian(xlim = c(-.4, .4)) +
  scale_fill_grey() + 
  mytheme

ggsave("vote_choice.pdf", width = 4, height = 4)


## vote choice predicted values -------------------------------------------

### Model 1: Agree
prediction(models_vote[[1]],
           at = list(dv_agree = c(0, weighted.mean(d$dv_agree, d$weight), .5, 1)))

#### Model 2: Angry
prediction(models_vote[[2]],
           at = list(dv_angry = c(0, weighted.mean(d$dv_angry, d$weight), .5, 1)))



# APPENDICES -----------------------------------------------------------------------

## appendix c ----------------------------------------------------------------------

### table c.1
modelsummary(models_glm[1:4],
             title = "Average Treatment Effects via MLE \\label{tab:logit_overall}",
             gof_omit = "edf",
             estimate = "{estimate}{stars}",
             stars = c('*' = .05, '**' = .01, '***' = .001),
             coef_map = cm,
             coef_omit = "pid",
             add_rows = tribble(~term, ~"1", ~"2", ~"3", ~"4",
                                "Std.Errors", "Robust", "Robust", "Robust", "Robust", 
                                "Covariate Adjustment?", "No", "Yes", "No", "Yes"),
             output = "latex") %>%
  kable_styling(latex_options = c("HOLD_position")) %>%
  add_header_above(c(" " = 1, "DV: Agree" = 2, "DV: Angry" = 2), italic = T) %>%
  footnote(general = c("Ordered logistic regression coefficients with standard errors in parentheses. Covariates: party identification, age, gender, race, education, income, employment status, marital status, immigration history, language, and region.",
                       "* p $<$ 0.05, ** p $<$ 0.01, *** p $<$ 0.001"),
           general_title = "Notes:",
           threeparttable = T,
           escape = F) %>%
  save_kable("logit_overall.tex")

### table c.2
modelsummary(models_glm[5:12],
             title = "Average Treatment Effects via MLE \\label{tab:logit_pid}",
             gof_omit = "edf",
             estimate = "{estimate}{stars}",
             stars = c('*' = .05, '**' = .01, '***' = .001),
             coef_map = cm,
             coef_omit = "pid",
             add_rows = tribble(~term, ~"1", ~"2", ~"3", ~"4", ~"1", ~"2", ~"3", ~"4",
                                "Std.Errors", "Robust", "Robust", "Robust", "Robust",  "Robust", "Robust", "Robust", "Robust",
                                "Covariate Adjustment?", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes"),
             output = "latex") %>%
  kable_styling(latex_options = c("HOLD_position", "scale_down")) %>%
  add_header_above(c(" " = 1, "Liberals Only" = 2, "Opposition Only" = 2, "Liberals Only" = 2, "Opposition Only" = 2)) %>%
  add_header_above(c(" " = 1, "DV: Agree" = 4, "DV: Angry" = 4), italic = T) %>%
  footnote(general = c("Ordered logistic regression coefficients with standard errors in parentheses. Covariates: age, gender, race, education, income, employment status, marital status, immigration history, language, and region.", 
                       "* p $<$ 0.05, ** p $<$ 0.01, *** p $<$ 0.001"),
           general_title = "Notes:",
           threeparttable = T,
           escape = F) %>%
  save_kable("logit_pid.tex")


## appendix d ------------------------------------------------------------------------

modelsummary(models_cate,
             title = "Conditional Average Treatment Effects \\label{tab:interactions}",
             estimate = "{estimate}{stars}",
             vcov = "robust",
             stars = c('*' = .05, '**' = .01, '***' = .001),
             gof_omit = "^(?!Num|R2 |Std)",
             coef_map = cm,
             coef_omit = "pid",
             add_rows = tribble(~term, ~"1", ~"2", ~"3", ~"4", ~"5", ~"6",
                                "Covariate Adjustment?", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
             output = "latex") %>%
  kable_styling(latex_options = c("HOLD_position", "scale_down")) %>%
  add_header_above(c(" " = 1, "DV: Agree" = 3, "DV: Angry" = 3), italic = T) %>%
  footnote(general = c("OLS regression coefficients with standard errors in parentheses. Covariates: party identification, age, gender, race, education, income, employment status, marital status, immigration history, language, and region.", 
                       "* p $<$ 0.05, ** p $<$ 0.01, *** p $<$ 0.001"),
           general_title = "Notes:",
           threeparttable = T,
           escape = F) %>%
  save_kable("interactions.tex")


## appendix e ------------------------------------------------------------------------

### table e.1
d_summary <- d %>% 
  drop_na(pid) %>%
  mutate(`Region: Antlantic` = ifelse(region == "Atlantic", 1, 0), 
         `Region: British Columbia` = ifelse(region == "British Columbia", 1, 0), 
         `Region: Ontario` = ifelse(region == "Ontario", 1, 0),
         `Region: Prariries` = ifelse(region == "Prairies", 1, 0), 
         `Region: Quebec` = ifelse(region == "Quebec", 1, 0)) %>%
  select(treatment, weight,
         Agree = dv_agree, Angry = dv_angry,
         Control = control, `T1: Info` = t1_info, `T2: Frame` = t2_frame,
         `Party ID (Opposition = 1)` = pid,
         Age = age, `Gender (Man = 1)` = gender, `Race (White = 1)` = race, 
         `Education Level` = education, `Income Group` = income, `Employment Status (Employed = 1)` = , `Marital Status (Married = 1)` = married,
         `Language (Francophone = 1)` = francophone, `Immigration History (Born Abroad = 1)` = immigrant,
         `Region: Antlantic`, `Region: British Columbia`, `Region: Ontario`, `Region: Prariries`, `Region: Quebec`,
         `Government Satisfaction` = satisfaction_trudeau, `Ideology: Left` = ideology_left, `Ideology: Center` = ideology_center, `Ideology: Right` = ideology_right,
         `Political Awareness Index` = awareness_index, `Political Cynicism Index` = cynicism_index, `Economic Evaluations Index` = economy_index)

d_summary %>%
  select(-all_of(c("Control", "T1: Info", "T2: Frame", "Government Satisfaction", "Ideology: Left", "Ideology: Center", "Ideology: Right", 
                   "Political Awareness Index", "Political Cynicism Index", "Economic Evaluations Index"))) %>%
  datasummary_balance(~treatment,
                      data = .,
                      fmt = 3,
                      title = "Sample Characteristics by Experimental Condition \\label{tab:balance}",
                      output = "latex") %>%
  row_spec(1:2, bold = T) %>%
  kable_styling(latex_options = c("hold_position")) %>%
  save_kable("balance.tex")

### figure e.1
p1 <- d %>%
  ggplot(aes(x = treatment, 
             fill = reorder(dv_agree_factor, desc(dv_agree_factor)))) + 
  geom_bar(position = "fill") + 
  scale_fill_grey() + 
  ylab("") +
  xlab("") +
  ggtitle("DV: Agree") +
  mytheme + 
  theme(legend.direction="vertical")

p2 <- d %>%
  ggplot(aes(x = treatment, 
             fill = reorder(dv_angry_factor, desc(dv_angry_factor)))) + 
  geom_bar(position = "fill") + 
  scale_fill_grey() + 
  ylab("") +
  xlab("") +
  ggtitle("DV: Angry") +
  mytheme + 
  theme(legend.direction="vertical")

p1 + p2

ggsave("dv_distribution.pdf", width = 5, height = 4)


## appendix f ----------------------------------------------------------------------
options(scipen=999)
`Missing (%)` <- \(x) sprintf("%.0f", weighted.mean(is.na(x)) * 100)
`Unique (#)` <- \(x, ...) sprintf("%.0f", length(unique(x[!is.na(x)])))
W.Mean <- \(x) weighted.mean(x, d_summary$weight, na.rm=TRUE)
W.SD <- \(x) weightedSd(x, d_summary$weight)
W.Median <- \(x) weightedMedian(x, d_summary$weight)

d_summary %>%
  select(-weight) %>%
  as.data.frame %>%
  datasummary(All(.) 
              ~ `Unique (#)`
              + `Missing (%)`
              + (Mean = W.Mean)
              + (SD = W.SD)
              + (Median = W.Median)
              + Min * Arguments(fmt = "%.0f")
              + Max * Arguments(fmt = "%.0f"),
              fmt = 4,
              data = .,
              title = "Summary Statistics \\label{tab:summary}",
              output = "latex") %>%
  kable_styling(latex_options = c("hold_position")) %>%
  save_kable("summary.tex")


## appendix g ----------------------------------------------------------------------

### table g.1
modelsummary(models_ols[1:4],
             title = "Average Treatment Effects \\label{tab:main}",
             estimate = "{estimate}{stars}",
             vcov = "robust",
             stars = c('*' = .05, '**' = .01, '***' = .001),
             gof_omit = "^(?!Num|R2 |Std)",
             coef_map = cm,
             coef_omit = "pid",
             add_rows = tribble(~term, ~"2", ~"2", ~"2", ~"4",
                                "Covariate Adjustment?", "No", "Yes", "No", "Yes"),
             output = "latex") %>%
  kable_styling(latex_options = c("HOLD_position")) %>%
  add_header_above(c(" " = 1, "DV: Agree" = 2, "DV: Angry" = 2), italic = T) %>%
  footnote(general = c("OLS regression coefficients with standard errors in parentheses. Covariates: party identification, age, gender, race, education, income, employment status, marital status, immigration history, language, and region.", 
                       "* p $<$ 0.05, ** p $<$ 0.01, *** p $<$ 0.001"),
           general_title = "Notes:",
           threeparttable = T,
           escape = F) %>%
  save_kable("main.tex")

### table g.2
modelsummary(models_ols[5:12],
             title = "Average Treatment Effects, among Partisan Subsets \\label{tab:pid}",
             estimate = "{estimate}{stars}",
             vcov = "robust",
             stars = c('*' = .05, '**' = .01, '***' = .001),
             gof_omit = "^(?!Num|R2 |Std)",
             coef_map = cm,
             coef_omit = "pid",
             add_rows = tribble(~term, ~"1", ~"2", ~"3", ~"4", ~"5", ~"6", ~"7", ~"8",
                                "Covariate Adjustment?", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes"),
             output = "latex") %>%
  kable_styling(latex_options = c("HOLD_position", "scale_down")) %>%
  add_header_above(c(" " = 1, "Liberals Only" = 2, "Opposition Only" = 2, "Liberals Only" = 2, "Opposition Only" = 2)) %>%
  add_header_above(c(" " = 1, "DV: Agree" = 4, "DV: Angry" = 4), italic = T) %>%
  footnote(general = c("OLS regression coefficients with standard errors in parentheses. Covariates: age, gender, race, education, income, employment status, marital status, immigration history, language, and region.", 
                       "* p $<$ 0.05, ** p $<$ 0.01, *** p $<$ 0.001"),
           general_title = "Notes:",
           threeparttable = T,
           escape = F) %>%
  save_kable("pid.tex")


## appendix h ----------------------------------------------------------------------

modelsummary(models_vote,
             title = "Vote Choice Linear Probability Models \\label{tab:vote}",
             estimate = "{estimate}{stars}",
             vcov = "robust",
             stars = c('*' = .05, '**' = .01, '***' = .001),
             gof_omit = "^(?!Num|R2 |Std)",
             coef_map = cm,
             add_rows = tribble(~term, ~"1", ~"2", ~"3", ~"4", ~"5", ~"6",
                                "Sociodemographic Controls?", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
             output = "latex") %>%
  kable_styling(latex_options = c("HOLD_position", "scale_down")) %>%
  add_header_above(c(" " = 1, "All Respondents" = 2, "Liberals Only" = 2, "Opposition Only" = 2)) %>%
  add_header_above(c(" " = 1, "DV: Vote Choice (Liberal=1, Opposition=0)" = 6), italic = T) %>%
  footnote(general = c("Sociodemographic controls: age, gender, race, education, income, employment status, marital status, immigration history, language, and region.",
                       "* p $<$ 0.05, ** p $<$ 0.01, *** p $<$ 0.001"),
           general_title = "Notes:",
           threeparttable = T,
           escape = F) %>%
  save_kable("vote.tex")


## appendix i ----------------------------------------------------------------------

d_non_pid <- filter(d, is.na(pid))

### table i.1
models_ols_no_pid <- list(
  lm(dv_agree ~ t1_info + t2_frame, 
     weights = weight, data = d_non_pid),
  lm(dv_agree ~ t1_info + t2_frame
     + age + gender + race + education + income + married
     + employment + immigrant + francophone + region,
     weights = weight, data = d_non_pid),
  lm(dv_angry ~ t1_info + t2_frame, 
     weights = weight, data = d_non_pid),
  lm(dv_angry ~ t1_info + t2_frame
     + age + gender + race + education + income + married
     + employment + immigrant + francophone + region, 
     weights = weight, data = d_non_pid)
)

modelsummary(models_ols_no_pid,
             title = "Average Treatment Effects among Non-Partisans \\label{tab:main_no_pid}",
             estimate = "{estimate}{stars}",
             vcov = "robust",
             stars = c('*' = .05, '**' = .01, '***' = .001),
             gof_omit = "^(?!Num|R2 |Std)",
             coef_map = cm,
             coef_omit = "pid",
             add_rows = tribble(~term, ~"2", ~"2", ~"2", ~"4",
                                "Covariate Adjustment?", "No", "Yes", "No", "Yes"),
             output = "latex") %>%
  kable_styling(latex_options = c("HOLD_position")) %>%
  add_header_above(c(" " = 1, "DV: Agree" = 2, "DV: Angry" = 2), italic = T) %>%
  footnote(general = c("OLS regression coefficients with standard errors in parentheses. Covariates: age, gender, race, education, income, employment status, marital status, immigration history, language, and region.", 
                       "* p $<$ 0.05, ** p $<$ 0.01, *** p $<$ 0.001"),
           general_title = "Notes:",
           threeparttable = T,
           escape = F) %>%
  save_kable("main_no_pid.tex")

### table i.2
models_vote_no_pid <- list(
  lm(vote_post ~ dv_agree 
     + t1_info + t2_frame
     + ideology_left + ideology_right
     + satisfaction_trudeau
     + age + gender + race + education + income + married
     + employment + immigrant + francophone + region, 
     weights = weight, data = d_non_pid),
  lm(vote_post ~ dv_angry 
     + t1_info + t2_frame 
     + ideology_left + ideology_right
     + satisfaction_trudeau
     + age + gender + race + education + income + married
     + employment + immigrant + francophone + region, 
     weights = weight, data = d_non_pid)
)

modelsummary(models_vote_no_pid,
             title = "Vote Choice Linear Probability Models among Non-Partisans \\label{tab:vote_no_pid}",
             estimate = "{estimate}{stars}",
             vcov = "robust",
             stars = c('*' = .05, '**' = .01, '***' = .001),
             gof_omit = "^(?!Num|R2 |Std)",
             coef_map = cm,
             add_rows = tribble(~term, ~"1", ~"2",
                                "Sociodemographic Controls?", "Yes", "Yes"),
             output = "latex") %>%
  kable_styling(latex_options = c("HOLD_position")) %>%
  add_header_above(c(" " = 1, "DV: Vote Choice (Liberal=1, Opposition=0)" = 2), italic = T) %>%
  footnote(general = c("Sociodemographic controls: age, gender, race, education, income, employment status, marital status, immigration history, language, and region.",
                       "* p $<$ 0.05, ** p $<$ 0.01, *** p $<$ 0.001"),
           general_title = "Notes:",
           threeparttable = T,
           escape = F) %>%
  save_kable("vote_no_pid.tex")







# R version 4.3.2 (2023-10-31 ucrt)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows 11 x64 (build 22621)
# 
# Matrix products: default
# 
# 
# locale:
# [1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8    LC_MONETARY=English_United States.utf8
# [4] LC_NUMERIC=C                           LC_TIME=English_United States.utf8    
# 
# time zone: America/New_York
# tzcode source: internal
# 
# attached base packages:
# [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
# [1] lubridate_1.9.3    forcats_1.0.0      stringr_1.5.1      dplyr_1.1.4        purrr_1.0.2        readr_2.1.4       
# [7] tidyr_1.3.0        tibble_3.2.1       ggplot2_3.4.4      tidyverse_2.0.0    kableExtra_1.3.4   matrixStats_1.1.0 
# [13] patchwork_1.1.3    sandwich_3.0-2     MASS_7.3-60        margins_0.3.26     lmtest_0.9-40      zoo_1.8-12        
# [19] modelsummary_1.4.3 scales_1.3.0       haven_2.5.4       
# 
# loaded via a namespace (and not attached):
# [1] gtable_0.3.4       bayestestR_0.13.1  prediction_0.3.14  xfun_0.41          htmlwidgets_1.6.4  insight_0.19.7    
# [7] lattice_0.21-9     tzdb_0.4.0         vctrs_0.6.5        tools_4.3.2        generics_0.1.3     datawizard_0.9.0  
# [13] fansi_1.0.5        pkgconfig_2.0.3    data.table_1.14.8  checkmate_2.3.1    webshot_0.5.5      lifecycle_1.0.4   
# [19] farver_2.1.1       compiler_4.3.2     textshaping_0.3.7  munsell_0.5.0      estimatr_1.0.0     htmltools_0.5.7   
# [25] Formula_1.2-5      pillar_1.9.0       DT_0.30            tidyselect_1.2.0   rvest_1.0.3        digest_0.6.33     
# [31] performance_0.10.8 stringi_1.8.2      labeling_0.4.3     fastmap_1.1.1      grid_4.3.2         colorspace_2.1-0  
# [37] cli_3.6.1          magrittr_2.0.3     utf8_1.2.4         withr_2.5.2        backports_1.4.1    timechange_0.2.0  
# [43] rmarkdown_2.25     httr_1.4.7         ragg_1.2.6         hms_1.1.3          evaluate_0.23      knitr_1.45        
# [49] parameters_0.21.3  viridisLite_0.4.2  rlang_1.1.2        Rcpp_1.0.11        glue_1.6.2         xml2_1.3.6        
# [55] svglite_2.1.2      rstudioapi_0.15.0  R6_2.5.1           tables_0.9.17      systemfonts_1.0.5 